<template>
  <div>
    <van-nav-bar
  title="牛牛头条-登录"
/>
<van-form @submit="onSubmit">
  <van-field
  autocomplete="true"
    v-model="user.mobile"
    name="mobile"
    label="手机号"
    placeholder="手机号"
    required
    :rules="[{pattern, required: true, message: '请填写正确的手机号' }]"
  />
  <van-field
    v-model="user.code"
    required
    type="password"
    name="code"
    label="密码"
    placeholder="密码"
    :rules="[{ pattern:/^\d{6}$/,required: true, message: '请填写正确密码' }]"
  />
  <div style="margin: 16px;">
    <van-button :disabled='isloading' round loading-text="登录中..." :loading='isloading' block type="info" native-type="submit">登录</van-button>
  </div>
</van-form>
  </div>
</template>

<script>
import { login } from '@/api'
import { Notify } from 'vant'
import { setToken } from '@/utils/token'
import Vue from 'vue'
Vue.use(Notify)
export default {
  data () {
    return {
      user: {
        mobile: '13888888888', // 手机号
        code: '246810'// 密码，没有验证码接口用密码代替
      },
      pattern: /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/,
      isloading: false
    }
  },
  methods: {
    async onSubmit (values) {
      this.isloading = true
      try {
        const result = await login(values)
        Notify({ type: 'success', message: '登陆成功' })
        setToken(result.data.data.token)
        this.$router.replace({ path: '/layout/home' })
      } catch (error) {
        Notify({ type: 'danger', message: '账号或密码错误', background: 'red' })
      }
      this.isloading = false
    }
  }
}
</script>

<style scoped lang='less'>
/* 使用选择器进行属性修改，不推荐，推荐使用vant自带的方式在vue.config内修改 */
/* vant NavBar根标签 */
/* .van-nav-bar{
  background:#007bff;
} */
/* 组件库内部的class属性查找使用/deep/使得可以依靠后代选择器进行查找 */
/* /deep/ .van-nav-bar__title{
  color:white
} */
</style>
